package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.SecneMode;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class SecneModeDao {
    private Context mContext;
    private Dao<SecneMode, Integer> secneDaoOpe;
    private DatabaseHelper helper;

    public SecneModeDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            secneDaoOpe = helper.getDao(SecneMode.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(SecneMode secne) {
        try {
            secne.setChecked(false);
            secneDaoOpe.create(secne);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateSecneMode(SecneMode secne) {
        try {
//            secne.setType(false);
            return secneDaoOpe.update(secne);
            //    LogHelper.i("updateSecneModenumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public SecneMode get(int id) {
        try {
            return secneDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public SecneMode getSelect() {
        try {
            List<SecneMode> families = getAll();
            if (families.size() < 1) return null;
            for (SecneMode secne : families) {
                if (secne.isChecked()) return secne;
            }
            SecneMode secne = families.get(0);
            secne.setChecked(true);
            updateSecneMode(secne);
            return secne;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<SecneMode> getAll() {
        List<SecneMode> secneList = null;
        try {
            secneList = secneDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return secneList;
    }

    public SecneMode getById(String secneId) {
        try {
            List<SecneMode> secneList = secneDaoOpe.queryBuilder()
                    .where().eq
                            ("secneId", secneId).query();
            if (secneList != null && secneList.size() > 0) {
                return secneList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<SecneMode> getFamilySecnes(String familyId) {
        List<SecneMode> secneList = new ArrayList<>();
        try {
            secneList = secneDaoOpe.queryBuilder()
                    .where().eq("familyId", familyId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return secneList;

    }

    public SecneMode getSecneByName(String familyId, String name) {
        try {
            List<SecneMode> secneList = secneDaoOpe.queryBuilder()
                    .where().eq("familyId", familyId).and().eq
                            ("name", name).query();
            if (secneList != null && secneList.size() > 0) {
                return secneList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    public int delete(SecneMode secne) {
        try {
            return secneDaoOpe.delete(secne);
            //    LogHelper.i("deleSecneModenumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public void deleteFamilySecneMode(String familyId) {
        try {
            List<SecneMode> familySecnes = getFamilySecnes(familyId);
            int number = secneDaoOpe.delete(familySecnes);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }



    public void deleAll() {
        try {
            int number = secneDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<SecneMode> secneList) {
        try {
            int number = secneDaoOpe.delete(secneList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
